/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__find_pair_str.c                                 */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/15 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include "../../../../src/module/oracle/config.h"
#include <oracle/ora_core.h>
#include <oracle/comm/ora_comm.h>

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

int __find_pair_str(char *p_data, char **pp_pair_str, int *pi_pair_str_len);

static void __case1(void)
{
    int i_ret = 0;
    int i_output = 0;
    char *p_data = NULL;
    char *p_output = NULL;

    i_ret = __find_pair_str(p_data, &p_output, &i_output);
    CU_ASSERT_EQUAL(i_ret, FR_FAIL);
}

static void __case2(void)
{
    int i_ret = 0;
    int i_output = 0;
    char *p_data = NULL;
    char *p_output = NULL;
    
    p_data = (char *) frmalloc(strlen("(DESCRIPTION)") + 1);
    snprintf(p_data, strlen("(DESCRIPTION)") + 1, "%s", "(DESCRIPTION)");

    i_ret = __find_pair_str(p_data, &p_output, &i_output);
    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_output, "(DESCRIPTION)");
    frfree(p_data);
    frfree(p_output);
}

static void __case3(void)
{
    int i_ret = 0;
    int i_output = 0;
    char *p_data = NULL;
    char *p_output = NULL;
    
    p_data = (char *) frmalloc(strlen("D(E(SCRIPTI)O)N") + 1);
    snprintf(p_data, strlen("D(E(SCRIPTI)O)N") + 1, "%s", "D(E(SCRIPTI)O)N");

    i_ret = __find_pair_str(p_data, &p_output, &i_output);
    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_output, "(E(SCRIPTI)O)");
    frfree(p_data);
    frfree(p_output);
}

//没有')'，返回成功
static void __case4(void)
{
    int i_ret = 0;
    int i_output = 0;
    char *p_data = NULL;
    char *p_output = NULL;
    
    p_data = (char *) frmalloc(strlen("(DE(SCRIPTION") + 1);
    snprintf(p_data, strlen("(DE(SCRIPTION") + 1, "%s", "(DE(SCRIPTION");

    i_ret = __find_pair_str(p_data, &p_output, &i_output);
    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_PTR_NULL(p_output);
    frfree(p_data);
}

int add_suite__find_pair_str(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __find_pair_str()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
